import pandas as pd
import numpy as np
import re
import os
import sys


MAX_INT=sys.maxsize

#Dpath1 txt Dpath2 csv,Dpath3 feature
def txt_to_csv(Dpath1,Dpath2,Dpath3):
    f=open(Dpath1,'r')
    line = f.readline()  
    list1 = []
    while line:
        a = re.split('[ ;;,:]',line.strip())
        b = a[0:MAX_INT]       # 这是选取需要读取的列
        list1.append(b)  # 将其添加在列表之中
        line = f.readline()
    f.close()

    test=pd.DataFrame(data=list1)

    df=test.rename(columns={0:'order_id',1:'ata',2:'distance',3:
                                'simple_eta',4:'driver_id',5:'slice_id'})


    #df1=pd.read_csv('temp3.csv')
    df1=df
    #df2=df1.sort_values(by='slice_id')
    df2=df1
    slice=df2['slice_id']
    del df2['slice_id']
    df2.insert(1,'slice_id',slice)
    del df2['order_id']
    del df2['driver_id']

    df2.to_csv(Dpath2)
    #df2.to_csv('test4.csv')

    #path3='/mnt/715/temp4.csv'
    
    df3=pd.read_csv(Dpath2,index_col=0,header=None)

    df3=df3.fillna(-1)

    array_0 = np.zeros([df3.shape[0],48])

    for i in range(1,df3.shape[0]-1):
    #for i in range(1,2):
            #for j in range(0,df3.shape[1],5):
        for j in range(14,df3.shape[1]-1,5):
                
                if float(df3[j][i])==0:
                    array_0[i-1][0]=array_0[i-1][0]+1
                    array_0[i-1][1]=array_0[i-1][1]+float(df3[j-2][i]) 
                
                if(float(df3[j][i])==1 and float(df3[j+1][i])==0):
                    array_0[i-1][2]=array_0[i-1][2]+1
                    array_0[i-1][3]=array_0[i-1][3]+float(df3[j-2][i])                 
                if(float(df3[j][i])==1 and float(df3[j+1][i])==1):
                    array_0[i-1][4]=array_0[i-1][4]+1
                    array_0[i-1][5]=array_0[i-1][5]+float(df3[j-2][i]) 
                if(float(df3[j][i])==1 and float(df3[j+1][i])==2):
                    array_0[i-1][6]=array_0[i-1][6]+1
                    array_0[i-1][7]=array_0[i-1][7]+float(df3[j-2][i]) 
                if(float(df3[j][i])==1 and float(df3[j+1][i])==3):
                    array_0[i-1][8]=array_0[i-1][8]+1
                    array_0[i-1][9]=array_0[i-1][9]+float(df3[j-2][i]) 
                if(float(df3[j][i])==1 and float(df3[j+1][i])==4):
                    array_0[i-1][10]=array_0[i-1][10]+1
                    array_0[i-1][11]=array_0[i-1][11]+float(df3[j-2][i])

                if(float(df3[j][i])==2 and float(df3[j+1][i])==0):
                    array_0[i-1][12]=array_0[i-1][12]+1
                    array_0[i-1][13]=array_0[i-1][13]+float(df3[j-2][i]) 
                if(float(df3[j][i])==2 and float(df3[j+1][i])==1):
                    array_0[i-1][14]=array_0[i-1][14]+1
                    array_0[i-1][15]=array_0[i-1][15]+float(df3[j-2][i]) 
                if(float(df3[j][i])==2 and float(df3[j+1][i])==2):
                    array_0[i-1][16]=array_0[i-1][16]+1
                    array_0[i-1][17]=array_0[i-1][17]+float(df3[j-2][i]) 
                if(float(df3[j][i])==2 and float(df3[j+1][i])==3):
                    array_0[i-1][18]=array_0[i-1][18]+1
                    array_0[i-1][19]=array_0[i-1][19]+float(df3[j-2][i]) 
                if(float(df3[j][i])==2 and float(df3[j+1][i])==4):
                    array_0[i-1][20]=array_0[i-1][20]+1
                    array_0[i-1][21]=array_0[i-1][21]+float(df3[j-2][i])
                    
                if(float(df3[j][i])==3 and float(df3[j+1][i])==0):
                    array_0[i-1][22]=array_0[i-1][22]+1
                    array_0[i-1][23]=array_0[i-1][23]+float(df3[j-2][i]) 
                if(float(df3[j][i])==3 and float(df3[j+1][i])==1):
                    array_0[i-1][24]=array_0[i-1][24]+1
                    array_0[i-1][25]=array_0[i-1][25]+float(df3[j-2][i]) 
                if(float(df3[j][i])==3 and float(df3[j+1][i])==2):
                    array_0[i-1][26]=array_0[i-1][26]+1
                    array_0[i-1][27]=array_0[i-1][27]+float(df3[j-2][i]) 
                if(float(df3[j][i])==3 and float(df3[j+1][i])==3):
                    array_0[i-1][28]=array_0[i-1][28]+1
                    array_0[i-1][29]=array_0[i-1][29]+float(df3[j-2][i])
                if(float(df3[j][i])==3 and float(df3[j+1][i])==4):
                    array_0[i-1][30]=array_0[i-1][30]+1
                    array_0[i-1][31]=array_0[i-1][31]+float(df3[j-2][i])
                    
                if(float(df3[j][i])==4 and float(df3[j+1][i])==0):
                    array_0[i-1][32]=array_0[i-1][32]+1
                    array_0[i-1][33]=array_0[i-1][33]+float(df3[j-2][i]) 
                if(float(df3[j][i])==4 and float(df3[j+1][i])==1):
                    array_0[i-1][34]=array_0[i-1][34]+1
                    array_0[i-1][35]=array_0[i-1][35]+float(df3[j-2][i]) 
                if(float(df3[j][i])==4 and float(df3[j+1][i])==2):
                    array_0[i-1][36]=array_0[i-1][36]+1
                    array_0[i-1][37]=array_0[i-1][37]+float(df3[j-2][i]) 
                if(float(df3[j][i])==4 and float(df3[j+1][i])==3):
                    array_0[i-1][38]=array_0[i-1][38]+1
                    array_0[i-1][39]=array_0[i-1][39]+float(df3[j-2][i])
                if(float(df3[j][i])==4 and float(df3[j+1][i])==4):
                    array_0[i-1][40]=array_0[i-1][40]+1
                    array_0[i-1][41]=array_0[i-1][41]+float(df3[j-2][i])
                    

    for i in range(1,df3.shape[0]-1):
        for j in range(14,df3.shape[1]-1,5):
            m=0
            if(df3[j+7][i]==-1):
                m=j+7
                #print(m)
                break
        for n in range(m+2,df3.shape[1]-1,2):
            if df3[n][i]!=-1:
                array_0[i-1][46]=array_0[i-1][46]+1
                array_0[i-1][47]=array_0[i-1][47]+float(df3[n][i])
            else:
                break


    for i in range(1,df3.shape[0]-1):
        for j in range(14,df3.shape[1]-1,5):
            m=0
            if(df3[j+7][i]==-1):
                m=j+3
                #print(m)
                break
        for n in range(m,m+4):
            #for k in range(4):
                #print(df3[n][i])
                    array_0[i-1][n-m+42]=float(df3[n][i])
                
    #print(array_0)

    df4=pd.DataFrame(columns=['slice_id','ata','distance','simple_eta',
        'beg link time','beg link ratio','beg current status','beg arrival status','a0','time0',
        'a10','time10','a11','time11','a12','time12','a13','time13','a14','time14',
        'a20','time20','a21','time21','a22','time22','a23','time23','a24','time24',
        'a30','time30','a31','time31','a32','time32','a33','time33','a34','time34',
        'a40','time40','a41','time41','a42','time42','a43','time43','a44','time44',
        'end link time','end link ratio','end current status','end arrival status',
        'cross','corsstime',],
                    index=list(range(0,df3.shape[0])))



    df4[['slice_id','ata','distance','simple_eta','beg link time','beg link ratio',
        'beg current status','beg arrival status']]=df3[[1,2,3,4,7,8,9,10]]

    #array_0=np.delete(array_0,198,0)

    df4[['a0','time0',
        'a10','time10','a11','time11','a12','time12','a13','time13','a14','time14',
        'a20','time20','a21','time21','a22','time22','a23','time23','a24','time24',
        'a30','time30','a31','time31','a32','time32','a33','time33','a34','time34',
        'a40','time40','a41','time41','a42','time42','a43','time43','a44','time44',
        'end link time','end link ratio','end current status','end arrival status',
        'cross','corsstime']]=array_0

    ata=df4['ata']
    df4=df4.drop('ata',axis=1)
    df4.insert(0,'ata',ata)

    #df4=df4.drop(198, axis=0)
    #df4.to_csv('710_1.csv',index=False)
    #df4.to_csv('/mnt/715/train/0801.csv',index=False)
    df4.to_csv(Dpath3,index=False)




if __name__ == '__main__':
    #txt_to_csv('/mnt/g/learn/GIS/2021GISCUP/LSTM/715/temp.txt','/mnt/g/learn/GIS/2021GISCUP/LSTM/715/temp/temp1.csv','/mnt/g/learn/GIS/2021GISCUP/LSTM/715/csv/csv1.csv')
    txt_to_csv('/mnt/715/20200901.txt','/mnt/715/temp/temp_0901.csv','/mnt/715/temp/feature_0901.csv')

    txt_to_csv('/mnt/715/train/20200801.txt','/mnt/715/temp/temp_01.csv','/mnt/715/temp/feature_0801.csv')
    txt_to_csv('/mnt/715/train/20200802.txt','/mnt/715/temp/temp_02.csv','/mnt/715/temp/feature_0802.csv')
    txt_to_csv('/mnt/715/train/20200803.txt','/mnt/715/temp/temp_03.csv','/mnt/715/temp/feature_0803.csv')
    txt_to_csv('/mnt/715/train/20200804.txt','/mnt/715/temp/temp_04.csv','/mnt/715/temp/feature_0804.csv')
    txt_to_csv('/mnt/715/train/20200805.txt','/mnt/715/temp/temp_05.csv','/mnt/715/temp/feature_0805.csv')
    txt_to_csv('/mnt/715/train/20200806.txt','/mnt/715/temp/temp_06.csv','/mnt/715/temp/feature_0806.csv')
    txt_to_csv('/mnt/715/train/20200807.txt','/mnt/715/temp/temp_07.csv','/mnt/715/temp/feature_0807.csv')
    txt_to_csv('/mnt/715/train/20200808.txt','/mnt/715/temp/temp_08.csv','/mnt/715/temp/feature_0808.csv')
    txt_to_csv('/mnt/715/train/20200809.txt','/mnt/715/temp/temp_09.csv','/mnt/715/temp/feature_0809.csv')
    txt_to_csv('/mnt/715/train/202008010.txt','/mnt/715/temp/temp_10.csv','/mnt/715/temp/feature_0810.csv')
    txt_to_csv('/mnt/715/train/202008011.txt','/mnt/715/temp/temp_11.csv','/mnt/715/temp/feature_0811.csv')
    txt_to_csv('/mnt/715/train/202008012.txt','/mnt/715/temp/temp_12.csv','/mnt/715/temp/feature_0812.csv')
    txt_to_csv('/mnt/715/train/202008013.txt','/mnt/715/temp/temp_13.csv','/mnt/715/temp/feature_0813.csv')
    txt_to_csv('/mnt/715/train/202008014.txt','/mnt/715/temp/temp_14.csv','/mnt/715/temp/feature_0814.csv')
    txt_to_csv('/mnt/715/train/202008015.txt','/mnt/715/temp/temp_15.csv','/mnt/715/temp/feature_0815.csv')
    txt_to_csv('/mnt/715/train/202008016.txt','/mnt/715/temp/temp_16.csv','/mnt/715/temp/feature_0816.csv')
    txt_to_csv('/mnt/715/train/202008017.txt','/mnt/715/temp/temp_17.csv','/mnt/715/temp/feature_0817.csv')
    txt_to_csv('/mnt/715/train/202008018.txt','/mnt/715/temp/temp_18.csv','/mnt/715/temp/feature_0818.csv')
    txt_to_csv('/mnt/715/train/202008019.txt','/mnt/715/temp/temp_18.csv','/mnt/715/temp/feature_0819.csv')
    txt_to_csv('/mnt/715/train/202008020.txt','/mnt/715/temp/temp_20.csv','/mnt/715/temp/feature_0820.csv')
    txt_to_csv('/mnt/715/train/202008021.txt','/mnt/715/temp/temp_21.csv','/mnt/715/temp/feature_0821.csv')
    txt_to_csv('/mnt/715/train/202008022.txt','/mnt/715/temp/temp_22.csv','/mnt/715/temp/feature_0822.csv')
    txt_to_csv('/mnt/715/train/202008023.txt','/mnt/715/temp/temp_23.csv','/mnt/715/temp/feature_0823.csv')
    txt_to_csv('/mnt/715/train/202008024.txt','/mnt/715/temp/temp_24.csv','/mnt/715/temp/feature_0824.csv')
    txt_to_csv('/mnt/715/train/202008025.txt','/mnt/715/temp/temp_25.csv','/mnt/715/temp/feature_0825.csv')
    txt_to_csv('/mnt/715/train/202008026.txt','/mnt/715/temp/temp_26.csv','/mnt/715/temp/feature_0826.csv')
    txt_to_csv('/mnt/715/train/202008027.txt','/mnt/715/temp/temp_27.csv','/mnt/715/temp/feature_0827.csv')
    txt_to_csv('/mnt/715/train/202008028.txt','/mnt/715/temp/temp_28.csv','/mnt/715/temp/feature_0828.csv')
    txt_to_csv('/mnt/715/train/202008029.txt','/mnt/715/temp/temp_29.csv','/mnt/715/temp/feature_0829.csv')
    txt_to_csv('/mnt/715/train/202008030.txt','/mnt/715/temp/temp_30.csv','/mnt/715/temp/feature_0830.csv')
    txt_to_csv('/mnt/715/train/202008031.txt','/mnt/715/temp/temp_31.csv','/mnt/715/temp/feature_0831.csv')


    
    
    
    
    
    
    
    
    
    
    
    
    